
package common.packet;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


/**
 * Stores information about a client.
 *
 * @author xissburg
 */
public class ClientInfo implements Packet
{

    
    /*
     * The team this client is into. Makes no sense while the client is a spectator.
     */
    public static enum Team { LEFT, RIGHT }

    /**
     * Players are in the match, spectators are just watching.
     */
    public static enum Type { PLAYER, SPECTATOR }

    /**
     * User name
     */
    private String name;

    /**
     * [x,y] position the button of this client starts the match at.
     */
    private float startX, startY;
    private Team team;
    private Type type;

    /**
     * Whether this client is the owner of the room or not.
     */
    private boolean owner;

    //TODO: include database information

    
    public ClientInfo()
    {
        this("");
    }

    public ClientInfo(String name) {
        this.name = name;
        startX = startY = 0.f;
        team = Team.LEFT;
        type = Type.SPECTATOR;
        owner = false;
    }

    public ClientInfo(ClientInfo ci) {
        set(ci);
    }

    public ClientInfo(DataInputStream in) throws IOException {
        this();
        read(in);
    }

    //Two ClientInfo are considered equal if the name is equal.
    @Override
    public boolean equals(Object o)
    {
        return (o!=null) && (o instanceof ClientInfo) &&
                (this.name.equals(((ClientInfo)o).name));
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 31 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    public void read(DataInputStream in) throws IOException
    {
        name = in.readUTF();
        startX = in.readFloat();
        startY = in.readFloat();
        team = Team.valueOf(in.readUTF());
        type = Type.valueOf(in.readUTF());
        owner = in.readBoolean();
    }

    public void write(DataOutputStream out) throws IOException
    {
        out.writeUTF(getCode().name());
        out.writeUTF(name);
        out.writeFloat(startX);
        out.writeFloat(startY);
        out.writeUTF(team.name());
        out.writeUTF(type.name());
        out.writeBoolean(owner);
    }

    public Code getCode() {
        return Code.CLIENT_INFO;
    }

    public ClientInfo copy() {
        return new ClientInfo(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isOwner() {
        return owner;
    }

    public void setOwner(boolean owner) {
        this.owner = owner;
    }

    public float getStartX() {
        return startX;
    }

    public void setStartX(float startX) {
        this.startX = startX;
    }

    public float getStartY() {
        return startY;
    }

    public void setStartY(float startY) {
        this.startY = startY;
    }

    public void setStartXY(float startX, float startY) {
        this.startX = startX;
        this.startY = startY;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    /**
     * Overrides all the data of this ClientInfo with the data of the argument.
     * @param ci
     */
    public void set(ClientInfo ci)
    {
        name = ci.name;
        startX = ci.startX;
        startY = ci.startY;
        team = ci.team;
        type = ci.type;
        owner = ci.owner;
    }
}
